package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.IWechatService;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.swing.text.TableView;
import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements IWechatService {


    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";


    @Value("${wechat.appId}")
    private String appid;

    @Value("${wechat.appSecret}")
    private String secret;


    /**
     * 获取openid
     * @param code
     * @return
     */
    @Override
    public String getOpenid(String code) {
        //设置请求路径
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        //设置访问微信服务端的请求参数
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        paramMap.put("js_code", code);
        paramMap.put("grant_type", "authorization_code");
        //得到返回结果
        String result = HttpRequest.get(url).form(paramMap).execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(result);
        Integer errcode = jsonObject.getInt("errcode");
        if (errcode!=null && errcode!=0){
            throw new BaseException("调用微信接口失败"+jsonObject.getStr("errmsg"));
        }
        String openid = jsonObject.getStr("openid");
        return openid;
    }

    /**
     * 封装公共参数
     * @return
     */
    private Map<String, Object> getAppConfig() {
        return null;
    }

    /**
     * 获取手机号
     * @param detailCode
     * @return
     */
    @Override
    public String getPhone(String detailCode) {
        String token = getToken();
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+token;
        String result = HttpRequest.post(url).body("{\"code\":\"" + detailCode + "\"}").execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(result);
        Integer errcode = jsonObject.getInt("errcode");
        if (errcode!=null && errcode!=0){
            throw new BaseException("调用微信接口失败"+jsonObject.getStr("errmsg"));
        }
        JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
        if (phoneInfo!=null){
            return phoneInfo.getStr("phoneNumber");
        }else {
            throw new BaseException("没有获取到手机号");
        }
    }

    /**
     * 获取token
     * @return
     */
    public String getToken() {

        String url = "https://api.weixin.qq.com/cgi-bin/token";
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        paramMap.put("grant_type", "client_credential");

        String result = HttpRequest.get(url).form(paramMap).execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(result);
        Integer errcode = jsonObject.getInt("errcode");
        if (errcode!=null && errcode!=0){
            throw new BaseException("调用微信接口失败"+jsonObject.getStr("errmsg"));
        }
        return jsonObject.getStr("access_token");
    }
}